<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Cookie;
class Cart extends Common
{
	
    public function index()
    {
        if(Cookie::has('userInfo')==false) {
			$data['code']=1004;
			$data['msg']="登录超时或未登录！请重新登录";
        }else{
			$userInfo=Cookie::get('userInfo');
			Cookie::set('userInfo',$userInfo,7200);
			$memberId=$userInfo['id'];
			$url=config('tdwApi')."/cart/list.jhtml?memberId=$memberId";
			$res=$this->http($url);
			$res=json_decode($res[0],true);
			$data=$res;
		}
		return view('cart/index',['data'=>$data]);		
	}
	public function addCart(){
		$productId=input('post.id');
		$quantity=input('post.quantity');
        if(Cookie::has('userInfo')==false) {
			$res['code']=1004;
			$res['msg']="登录超时！请重新登录";
        }else{
			$userInfo=Cookie::get('userInfo');
			Cookie::set('userInfo',$userInfo,7200);
			$memberId=$userInfo['id'];
			$url=config('tdwApi')."/cart/add.jhtml?memberId=$memberId&id=$productId&quantity=$quantity";
			$res=$this->http($url);
			$res=json_decode($res[0],true);						
		}
		echo json_encode($res);
	
	}
	public function delete(){
		$productId=input('post.id');
		$arrs = explode('-',$productId);
		array_pop($arrs);
        if(Cookie::has('userInfo')==false) {
			$res['code']=1004;
			$res['msg']="登录超时！请重新登录";
        }else{
			$userInfo=Cookie::get('userInfo');
			Cookie::set('userInfo',$userInfo,7200);
			$memberId=$userInfo['id'];
			$str='';
			foreach($arrs as $k => $v){
				$str.="productIds=$v&";
			}
			$url=config('tdwApi')."/cart/deleteProduct.jhtml?memberId=$memberId&$str";
			$res=$this->http($url);
			$res=json_decode($res[0],true);						
		}
		echo json_encode($res);		
	}
	public function submit(){
        if(Cookie::has('userInfo')==false) {
			$this->error('登录超时！请重新登录',url('index/Login/login',['jump'=>'cart']),'',3);
        }else{
			$items=$_POST['item'];
			$quantitys=$_POST['quantity'];
			$i = 0;
			$str = '';
			foreach($quantitys as $key => $value){
				if(isset($items[$key]))
				{
					$str .= '&cartItems%5B'.$i.'%5D.quantity='.$value.'&cartItems%5B'.$i.'%5D.product.id='.$key;
					$i++;				
				}
				
			}
			$userInfo=Cookie::get('userInfo');
			Cookie::set('userInfo',$userInfo,7200);
			$memberId=$userInfo['id'];
			$url=config('tdwApi')."/order/info.jhtml?memberId=$memberId$str";
			$res=$this->http($url);
			$res=json_decode($res[0],true);
			if($res['code']==1){
				$data=$res['data'];
				return view('cart/order',['data'=>$data]);
			}else{
				$this->error($res['msg'],url('index/Cart/index'),'',3);
			}
						
		}
	}
	public function getFreight(){
        if(Cookie::has('userInfo')==false) {
			$res['code']=1004;
			$res['msg']="登录超时！请重新登录";
        }else{
			$userInfo=Cookie::get('userInfo');
			Cookie::set('userInfo',$userInfo,7200);
			$memberId=$userInfo['id'];
			$id=input('post.id');
			$url=config('tdwApi')."/order/getFreight.jhtml?memberId=$memberId&areaId=$id";
			$res=$this->http($url);
			$res=json_decode($res[0],true);			
		}
		echo json_encode($res);		
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
